package com.wang.immutable;

import java.util.Iterator;
import java.util.List;

/**
 * List
 * @author yandong
 */
public class ImmutableDemo1 {
    public static void main(String[] args) {
        /*
         * 创建不可变的List集合
         * {"张三", "李四", "王五", "赵六"}
         */
        // 一旦创建完成之后, 是无法修改的, 只能进行查询操作
        List<String> list = List.of("张三", "李四", "王五", "赵六");

        // 逐个打印
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        System.out.println(list.get(2));
        System.out.println(list.get(3));

        System.out.println("===========================");

        // for-each遍历
        for (String name : list) {
            System.out.println(name);
        }

        System.out.println("===========================");

        // for循环遍历
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println("===========================");

        // 迭代器遍历
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }

}
